var ItemOperator = function (item_id) { 
    var self = this;
    self.hide_after_ajax = true;
    self.item_id = item_id;
    this.set_item_id = function(item_id){ self.item_id = item_id; return self;}
    this.set_hide = function(hide){ self.hide_after_ajax = hide; return self;}
    this._request = function(type_name, callback){
        var self = this;
        var _h = self.hide_after_ajax;
        $.post('/dashboard/cms/hot_trips/' + type_name + '/', {"id": self.item_id}, function(data){
            if (data.status) {
                if (_h) {
                    $('tr[data-id="' + self.item_id + '"] td').fadeOut('slow');
                }
            }
            else {
                alert(data.prompt);
            };        
            callback && callback();
        });
    }
    // 拒绝
    this.reject = function(btn) {
        var self = this;
        btn && btn.button('loading');
        self._request('denied', function(){
            btn && btn.button('reset');
            $('#modal-reject').modal('hide');
        });
        return self;
    };
    // 待定
    this.not_bad = function(btn){
        var self = this;
        btn && btn.button('loading');
        self._request('not_bad');
        return self;
    };
    // 通过
    this.good = function(btn){
        var self = this;
        btn && btn.button('loading');
        self._request('good');
        return self;
    },
    // 通过并精选
    this.good_and_feature = function(btn){
        var self = this;
        btn && btn.button('loading');
        self._request('featured');
        return self;
    },
    // 仅精选
    this.feature = function(btn){
        var self = this;
        btn && btn.button('loading');
        $.post('/dashboard/cms/hot_trips/featured/', {"id": self.item_id}, function(data){
            if (!data.status) {
                alert(data.prompt);
            };        
            var _html = $('#html_actions_featured').html();
            $('tr[data-id="' + self.item_id + '"] .div-actions').html(_html);
        });
        return self;
    },
    this.unfeature = function(btn){
        var self = this; 
        url = '/dashboard/cms/hot_trips/featured/' + self.item_id + '/delete/';
        $.post(url, function(data){
            if (!data.status) {
                alert(data.prompt);
            };
            var _html = $('#html_actions_unfeatured').html();
            $('tr[data-id="' + self.item_id + '"] .div-actions').html(_html);
        });
    }
}
var operator = new ItemOperator();

var hot_trip = {
    // 高亮显示排序按钮
    highlight_sort_buttons: function(sort_key){
        if (sort_key.indexOf('trip_last_modified') != -1) {
            $('.sortable-trip_last_modified').addClass('active');
            $('.sortable[href*="' + sort_key +'"]').parent().addClass('active');
        }
        else {
            $('.sortable[href*="' + sort_key +'"]').addClass('active');
        }
    },
    // 高亮显示banner处
    highlight_top_banner: function(name){
        if (name.indexOf('others') != -1) {
            $('li[data-hilight="others"]').addClass('active');
            $('li[data-hilight="' + name +'"]').addClass('active');
        }
        else {
            $('li[data-hilight="' + name +'"]').addClass('active');
        }
    },
    // 高亮目前的tag
    highlight_current_tag: function(tag){
        $('#filter_by_tag').val(tag);
    }
}

// 添加地点后的回调方法
var _callback_add_as_good = function(item_id){
    $.fancybox.close();
    var btn = $('tr[data-id="' + item_id + '"] .good');
    operator.good(btn);
}
// 添加地点后的回调方法，编辑摘要
var _callback_edit_featured_summary = function(item_id){
    url = '/dashboard/cms/hot_trips/' + item_id + '/featured_summary/?callback=_callback_add_as_featured_and_hide(' +  item_id + ')'
    $.fancybox(url, {
        type: 'iframe',
        width: 600
    });
}
// 编辑摘要后的回调方法，添加到精选
var _callback_add_as_featured_and_hide = function(item_id){
    $.fancybox.close();
    var btn = $('tr[data-id="' + item_id + '"] .good');
    operator.set_item_id(item_id).good_and_feature(btn);
}

// 编辑摘要后的回调方法，添加到精选
var _callback_add_as_featured = function(item_id){
    $.fancybox.close();
    var btn = $('tr[data-id="' + item_id + '"] .good');
    operator.set_item_id(item_id).feature(btn);
}
// 设置封面之后的回调
var _callback_update_cover = function(url){
    $('tr[data-id="' + operator.item_id + '"] img:first').attr('src', url).parent().attr('href', url);
    $.fancybox.close();
}

$(function(){
    $('.bt-has-tooltip').tooltip({'placement': 'bottom'});
    $('a.btn-change-cover').bind('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id);
        var url = '/dashboard/cms/hot_trips/' + item_id + '/cover/?callback=_callback_update_cover';
        $.fancybox(url, {
            type: 'iframe', 
            title: '修改封面',
            width: '80%',
            height: '80%',
            autoSize: false,
            helpers: {
                overlay: {
                    preload: false,
                    showEarly: true,
                }
            }
        });
        return false;
    });

    // bind functions
    $('.reject').live('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id)
        $('#modal-reject').modal('show');
    });
    $('.confirm-reject').button('reset').bind('click', function(){
        operator.reject($(this));
    });
    $('.not-bad').bind('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id).not_bad($(this));
    });
    $('.good').bind('click', function(){
        var item_id = $(this).parents('tr').data('id');
        var trip_id = $(this).parents('tr').data('trip-id');
        operator.set_item_id(item_id);
        url = '/dashboard/cms/featured_trips/add/' + trip_id + '/?callback=_callback_add_as_good(' + item_id + ')';
        $.fancybox(url, {
            type: 'iframe',
            width: 600
        });
    });
    $('.good_and_feature').bind('click', function(){
        var item_id = $(this).parents('tr').data('id');
        var trip_id = $(this).parents('tr').data('trip-id');
        operator.set_item_id(item_id);
        url = '/dashboard/cms/featured_trips/add/' + trip_id + '/?callback=_callback_edit_featured_summary(' + item_id + ')';
        $.fancybox(url, {
            type: 'iframe',
            width: 600
        });
    })
    $('.feature').live('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id);
        url = '/dashboard/cms/hot_trips/' + item_id + '/featured_summary/?callback=_callback_add_as_featured(' +  item_id + ')';
        $.fancybox(url, {
            type: 'iframe',
            width: 600
        });
    });
    $('.unfeature').live('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id).unfeature();
    });
    $('.change_summary').live('click', function(){
        var item_id = $(this).parents('tr').data('id');
        operator.set_item_id(item_id);
        url = '/dashboard/cms/hot_trips/' + item_id + '/featured_summary/?callback=$.fancybox.close()';
        $.fancybox(url, {
            type: 'iframe',
            width: 600
        });
    });
    // 搜索表单
    $('#form_search').bind('submit', function(){
        var text = $(this).find('input:text').val();
        if (text.length < 2) {
            BTdash.misc.info({message: '搜索关键字不能小于两个字！', level: 'error'});
            return false;
        }
        return true;
    });
    $('#reset_query').bind('click', function(){
        window.location.href = './';
        return false;
    });
    // 标签相关的方法
    $('.create_trip_tag').bind('change', function(){
        var val = $(this).val();
        if (val !== 'default') {
            var tag_name = $(this).find(':selected').text();
            var item_id = $(this).parents('tr').data('id');
            var jdom_select = $(this);
            var jdom_tags = $(this).parent().find('.trip_tags');

            var url = '/dashboard/cms/hot_trips/' + item_id + '/tags/';
            $.post(url, {'tag_id': val}, function(data){
                if (!data.status) {
                    alert(data.prompt);
                } else {
                    jdom_tags.append('<span class="label label-info">' + tag_name + 
                            ' | <a title="删除该标签" data-tag_id="' + val + '" class="remove_trip_tag" href="javascript:void(0)">X</a></span>');
                    jdom_select.val('default');
                };
            });
        } 
    });

    $('.remove_trip_tag').live('click', function(){
        var tag_id = $(this).data('tag_id');
        var item_id = $(this).parents('tr').data('id');
        var jdom_tag = $(this).parent();

        var url = '/dashboard/cms/hot_trips/' + item_id + '/tags/delete/';
        $.post(url, {'tag_id': tag_id}, function(data){
            if (!data.status) {
                alert(data.prompt);
            } else {
                jdom_tag.remove();
            };
        });
    });

    $('#filter_by_tag').bind('change', function(){
        var href = $(this).find(':selected').data('href'); 
        window.location.href = href;
    });
})
$(function(){
    $('#save_tag').live("click", function(){
        var tag = $("#tag_value").val();
        $.ajax({
            "url": "/dashboard/cms/hot_trips/add_tag/",
            "type": "POST",
            "dataType": "json",
            "data": {
                "tag": tag
            },
            success:function(data){
                if (data.status==="success") {
                    window.location.reload();
                }
            }
        })
    })
})
